/**
 * 
 */
package com.${ltdAbbr}.xiao.${module}.task.web.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.quartz.DateBuilder.IntervalUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.${ltdAbbr}.xiao.${module}.task.po.TaskTriggerBo;
import com.${ltdAbbr}.xiao.${module}.task.service.SchedulerService;

/**
 * @author wangxiaoran-ds
 *
 */
@Controller
@RequestMapping("/main/task")
public class TaskController {

    private static final Logger logger = LoggerFactory.getLogger(TaskController.class);

    @Autowired
    private SchedulerService schedulerService;

    /**
     * 任务列表页面
     * @param req
     * @return
     */
    @RequestMapping("/manage")
    public ModelAndView manage(){
        ModelAndView mav = new ModelAndView("tpl/main/task_manage");
        mav.addObject("intervalUnitList", JSON.toJSONString(Arrays.asList(IntervalUnit.values())));
        return mav;
    }

    /**
     * 暂停任务
     * @param jobTrigger
     * @return
     * @throws Exception 
     */
    @ResponseBody
    @RequestMapping("/list")
    public List<TaskTriggerBo> list() throws Exception{
        return this.schedulerService.queryTaskList();
    }

    /**
     * 暂停任务
     * @param jobTrigger
     * @return
     */
    @ResponseBody
    @RequestMapping("/pause")
    public Map<String, Object> pause(TaskTriggerBo jobTrigger){
        Map<String, Object> result = new HashMap<String, Object>();
        if(jobTrigger==null||StringUtils.isBlank(jobTrigger.getGroupName())||
                StringUtils.isBlank(jobTrigger.getTriggerName())){
            result.put("code", -1);
        }else{
            try{
                this.schedulerService.pause(jobTrigger);
                result.put("code", 1);
            }catch(Exception e){
                result.put("code", -500);
                result.put("message", e.getMessage());
                logger.error("暂停任务发生异常:"+e.getMessage(), e);
            }
        }
        return result;
    }

    /**
     * 恢复任务
     * @param jobTrigger
     * @return
     */
    @ResponseBody
    @RequestMapping("/resume")
    public Map<String, Object> resume(TaskTriggerBo jobTrigger){
        Map<String, Object> result = new HashMap<String, Object>();
        if(jobTrigger==null||StringUtils.isBlank(jobTrigger.getGroupName())||
                StringUtils.isBlank(jobTrigger.getTriggerName())){
            result.put("code", -1);
        }else{
            try{
                this.schedulerService.resume(jobTrigger);
                result.put("code", 1);
            }catch(Exception e){
                result.put("code", -500);
                result.put("message", e.getMessage());
                logger.error("恢复任务发生异常:"+e.getMessage(), e);
            }
        }
        return result;
    }

    /**
     * 立即运行任务
     * @param jobTrigger
     * @return
     */
    @ResponseBody
    @RequestMapping("/run")
    public Map<String, Object> run(TaskTriggerBo jobTrigger){
        Map<String, Object> result = new HashMap<String, Object>();
        if(jobTrigger==null||StringUtils.isBlank(jobTrigger.getGroupName())||
                StringUtils.isBlank(jobTrigger.getJobName())){
            result.put("code", -1);
        }else{
            try{
                this.schedulerService.run(jobTrigger);
                result.put("code", 1);
            }catch(Exception e){
                result.put("code", -500);
                result.put("message", e.getMessage());
                logger.error("运行任务发生异常:"+e.getMessage(), e);
            }
        }
        return result;
    }

    /**
     * 重新配置任务
     * @param jobTrigger
     * @return
     */
    @ResponseBody
    @RequestMapping("/reset")
    public Map<String, Object> reset(TaskTriggerBo jobTrigger){
        Map<String, Object> result = new HashMap<String, Object>();
        if(jobTrigger==null||StringUtils.isBlank(jobTrigger.getGroupName())||
                StringUtils.isBlank(jobTrigger.getTriggerName())){
            result.put("code", -1);
        }else{
            try{
                this.schedulerService.reset(jobTrigger);
                result.put("code", 1);
            }catch(Exception e){
                result.put("code", -500);
                result.put("message", e.getMessage());
                logger.error("重新配置任务发生异常:"+e.getMessage(), e);
            }
        }
        return result;
    }
}
